<?php
/**
 * @author dinglin
 * lumesse岗位对应关系
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Const_Flow');
class Oa_Job_Flow_InputLumesse extends Oa_Job_Abstract {

    private $writedb = "hroa_master";//写数据库
    private $error_item;
    public function run(){
        $this->log("lumesse--开始 ".date('Y-m-d H:i:s'));
        $this->_input();
        if($this->error_item){
            foreach($this->error_item as $val){
                $this->log("没有找到该用户：".join(',', $val));
            }
        }
        $this->log("lumesse--结束 ".date('Y-m-d H:i:s'));
    }
    private function _input(){
        $this->_get_file('lumesse');
        while($temp = fgetcsv($this->file)){
            $user = $this->_get_user_info($temp[2], $temp[1]);
            if($user){
                $user['lums_depart_ename']=$temp[6];
                $user['lums_depart_name'] =$temp[5];
                $user['lums_depart_id']   =0;
                $user['lums_pdepart_ename']=$temp[4];
                $user['lums_pdepart_name'] =$temp[3];
                $user['lums_pdepart_id']=0;
                $user['user_name'] =$temp[1];
                $user['user_email']=$temp[2];
                $this->_insert_lumesse($user);
            }
            $this->_sleep();
        }
    }
    /**
     * 获取用户id,和部门id
     */
    private function _get_user_info($user_email,$user_name){
        $user_email = trim($user_email);
        $sql = "SELECT user_id FROM user WHERE job_status=1 AND office_mail='".$user_email."'";
        $user = $this->get_db_row($sql,$this->writedb);
        if(isset($user['user_id']) && $user_email){
            $sql = "SELECT job_id FROM work WHERE user_id=".$user['user_id']." AND is_parttime=1 AND is_deleted=1";
            $work = $this->get_db_row($sql,$this->writedb);
            $sql = "SELECT department_id FROM job WHERE job_id=".$work['job_id']." AND is_deleted=1";
            $job  = $this->get_db_row($sql,$this->writedb);
            return array('user_id'=>$user['user_id'],'hroa_depart_id'=>$job['department_id']);
        }else{
            $this->error_item[] = array($user_name,$user_email);
            return array();
        }
    }
    /**
     * 写入数据
     */
    private function _insert_lumesse($data){
        $sql = "INSERT INTO lumesse(user_id,lums_depart_ename,lums_depart_name,lums_depart_id,lums_pdepart_ename,lums_pdepart_name,lums_pdepart_id,user_name,user_email,hroa_depart_id) VALUES(";
        $sql.="".$data['user_id'].',';
        $sql.="'".$data['lums_depart_ename']."',";
        $sql.="'".$data['lums_depart_name']."',";
        $sql.="".$data['lums_depart_id'].",";
        $sql.="'".$data['lums_pdepart_ename']."',";
        $sql.="'".$data['lums_pdepart_name']."',";
        $sql.="".$data['lums_pdepart_id'].",";
        $sql.="'".$data['user_name']."',";
        $sql.="'".$data['user_email']."',";
        $sql.="".$data['hroa_depart_id']."";
        $sql.=")";
        $this->execute( $sql,array(), $this->writedb);
    }
    /**
     * 重跑用
     */
    public function remove_all_lumesse(){
        $sql = "DELETE FROM lumesse ";
        $this->execute( $sql,array(), $this->writedb);
    }
    /**
     * 获取文件
     */
    private function _get_file($name){
        $file = dirname(__FILE__).'/data/'.$name.'.csv';
        if (!file_exists($file)) {
            $this->log($file."不存在,请输入正确的问题文件");exit;
        }
        $this->file = fopen($file,"r");
    }
    /**
     * 休息一下
     */
    private function _sleep(){
        if($this->i){
            $this->i++;
        }else{
            $this->i = 1;
        }
        if($this->i%20){}
        else{
            sleep(1);
        }
    }
}